<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>取派员设置</title>
    <link rel="stylesheet" href="/resources/css/formSelects-v4.css"/>
    <link rel="stylesheet" href="/resources/layui/css/layui.css">
    <script rel="stylesheet" src="/resources/layui/layui.js"></script>
    <script rel="stylesheet" src="/resources/js/jquery.min.js"></script>
    <script src="/resources/js/function/Local.js"></script>
</head>
<body>
<form class="layui-form" id="formone" action="">
    <div class="layui-inline">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-inline">
            <input type="text" name="name" id="xingming" placeholder="请输入员工名" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">所属状态</label>
        <div class="layui-input-inline">
            <select id="zts" xm-select="zts">
                <option value="">全部状态</option>
                <option value="-1">作废</option>
                <option value="0">待任命</option>
                <option value="1">任命</option>
            </select>
        </div>
    </div>
    <div class="layui-inline">
        <label class="layui-form-label">所属公司</label>
        <div class="layui-input-inline">
            <select id="gongs" xm-select="gongs">
                <option value="">全部公司</option>
            </select>
        </div>
    </div>
    <button type="button" class="layui-btn" onclick="getin()" lay-submit="" lay-filter="demo1">查询</button>
</form>
<table id="demo" lay-filter="test"></table>
</body>
</html>
<script>
    $.get(new Local().base_url+"/gongsi/show",function(data) {
        $.each(data.data, function () {
            var opt = $("<option></option>").appendTo("#gongs");
            opt.text(this.name).val(this.id);
        });
    });
</script>
<script>
    function getin(){
        var zts = layui.formSelects.value('zts','val');
        var gongs = layui.formSelects.value('gongs','val');
        //执行重载
        table.reload('demo', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                name : $("#xingming").val(),
                zts:zts.join(","),
                gongs :gongs.join(",")
            }
        });
    }
</script>
<script>
    var table,form,layer,laytpl,select;
    layui.config({
        base: '/resources/js/'
    }).extend({
        formSelects: 'formSelects-v4'
    });
    layui.use(['table','form','formSelects','laytpl','layer'], function(){
        table = layui.table;
        form=layui.form;
        select = layui.formSelects;
        layer=layui.layer;
        laytpl=layui.laytpl;
        //第一个实例
        table.render({
            elem: '#demo'
            ,url: new Local().base_url+'/user/list' //数据接口
            ,page: true //开启分页
            ,even : true
            ,toolbar:"#toolbarDemo"
            ,id : "demo"
            ,cols: [[ //表头
                {field:'id',type:'checkbox'}
                ,{field: 'id', title: 'ID'}
                ,{field: 'name', title: '姓名'}
                ,{field: 'phone', title: '手机号'}
                ,{field: 'zhuangtai', title: '状态',templet:function(d){
                    var str='作废';
                    if(d.zhuangtai==0){
                        str="待受命";
                    }
                    if(d.zhuangtai==1){
                        str="受命";
                    }
                    return str;
                }}
                ,{field: 'biaozhun', title: '取派标准'}
                ,{field: 'danweiId', title: '所谓单位',templet:function(d){
                    var str=" -=-";
                    if(d.gongsi!=null){
                        str=d.gongsi.name;
                    }
                    return str;
                }}
                ,{title:'操作',toolbar: '#barDemo'}
            ]]
        });

        table.on('tool(test)',function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            switch(layEvent){
                case "del":
                    layer.confirm('真的要作废么', function(index){
                        layer.close(index);
                        var zt=data.zhuangtai;
                        if(zt==-1){
                            layer.msg("已经作废,无需多次操作!");
                            return;
                        }
                        var id=data.id;
                        var str={
                            zhuangtai:-1,
                            id:id
                        };
                        $.post(new Local().base_url+"/user/qpUp",str,function(data) {
                            table.reload('demo');
                            layer.msg("作废成功!");
                        });
                    });
                    break;
                case "edit":
                    $.get('addstaff.html',function (s) {
                        //parent
                        layer.open({
                            type : 1,
                            title : '修改员工',
                            area:'70%',
                            anim: 1,
                            content:s,
                            btn : ['确定','取消'],
                            success:function(){
                                $("#id").val(data.id);
                                $("#mingzi").val(data.name);
                                $("#phone").val(data.phone);
                                $("#biaozhun").val(data.biaozhun);
                                var t =setInterval(function () {
                                    console.log("等待中....");
                                    if($("#required option").length > 1){
                                        var $se=$("#required");
                                        $se.val(data.danweiId);
                                        form.render();
                                        clearInterval(t);
                                    }
                                },100);
                                $("#hiddzhuangtai").val(0);
                                $("#zhuangtai_div").show();
                                if(data.zhuangtai==1){
                                    $("#zhuangtai").attr("checked", "checked");
                                }
                                form.render();
                            }
                            ,yes:function(){
                                var str = $("#form").serialize();
                                $.post(new Local().base_url+"/user/qvpaiupdate",str,function(data){
                                    layer.close(layer.index);
                                    table.reload('demo');
                                    layer.msg("修改成功!");
                                });
                            }
                        });
                    });
                    break;
                case "zan":
                    var zt=data.zhuangtai;
                    if(!(zt!=1)){
                        layer.msg("已上仍,无需多次上仍!");
                        return;
                    }
                    var id=data.id;
                    var str={
                        zhuangtai:1,
                        id:id
                    };
                    $.post(new Local().base_url+"/user/qpUp",str,function(data) {
                        table.reload('demo');
                        layer.msg("上仍成功!");
                    });
                    break;
                case "cai":
                    var zt=data.zhuangtai;
                    if(!(zt!=0)){
                        layer.msg("已撤销,无需多次撤销!");
                        return;
                    }
                    if(zt==-1){
                        layer.msg("还未上仍,无法撤销!");
                        return;
                    }
                    var id=data.id;
                    var str={
                        zhuangtai:0,
                        id:id
                    };
                    $.post(new Local().base_url+"/user/qpUp",str,function(data) {
                        table.reload('demo');
                        layer.msg("撤销成功!");
                    });
                    break;
            }
        });
        table.on('toolbar(test)', function(obj) {
            switch (obj.event) {
                case 'add':
                   $.get('addstaff.html',function (s) {
                       //parent
                        layer.open({
                            type : 1,
                            title : '添加员工',
                            anim: 1,
                            area:'70%',
                            content:s,
                            btn : ['确定','取消'],
                            success:function(){
                                form.render();
                            }
                            ,yes:function(){
                                var str = $("#form").serialize();
                                $.post(new Local().base_url+"/user/qvpaiadd",str,function(data){
                                    layer.close(layer.index);
                                    table.reload('demo');
                                    layer.msg("添加成功!");
                                });
                            }
                        });
                    });
                    break;
                case "batchDelete":
                    var idstr="";
                    var checkStatus = table.checkStatus('demo');
                    if(checkStatus.data.length<=0){
                        layer.msg("请选择后作废!");
                    }else{
                        for(let da of checkStatus.data){
                            idstr+=da.id+",";
                        }
                        $.post(new Local().base_url+"/user/deleqp",{idstr:idstr},function(data){
                            table.reload('demo');
                            layer.msg('作废成功!');
                        });
                    }
                    break;
            }
        });

    });
</script>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="add"><i class="layui-icon layui-icon-add-circle"></i> 添加</button>
        <button class="layui-btn layui-btn-sm" lay-event="batchDelete"><i class="layui-icon layui-icon-delete"></i> 批量删除</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a href="javascript:void(0)" lay-event="zan"><i class="layui-icon layui-icon-praise"></i></a>
    <a href="javascript:void(0)" lay-event="edit"><i class="layui-icon layui-icon-edit"></i></a>
    <a href="javascript:void(0)" lay-event="del"><i class="layui-icon layui-icon-delete"></i></a>
    <!--<a lay-event="map"><i class="layui-icon layui-icon-location"></i></a>-->
    <a href="javascript:void(0)" lay-event="cai"><i class="layui-icon layui-icon-tread"></i></a>
    <!--<a lay-event="list"><i class="layui-icon layui-icon-list"></i></a>-->
</script>
